<template>
  <div class="loginForm">
    <el-form :model="ruleForm2" status-icon :rules="rules2" ref="ruleForm2" class="demo-ruleForm">
      <el-form-item prop="username">
        <el-input type="text" v-model="ruleForm2.username" auto-complete="off"></el-input>
      </el-form-item>
      <el-form-item prop="password">
        <el-input type="password" v-model="ruleForm2.password" auto-complete="off"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button class="loginBtn" type="primary" @click="login">提交</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
export default {
  data() {
    let validateName = (rule, value, callback) => {
      let yanZheng = /^1[3-9][0-9]{9}$/.test(value);
      if (value) {
        callback();
      } else {
        callback(new Error("请输入正确格式的手机号"));
      }
    };
    return {
      ruleForm2: {
        username: "13800138000",
        password: "123456"
      },
      rules2: {
        username: [{ validator: validateName, trigger: "blur" }],
        password: [{ required: true, message: "请输入密码", trigger: "blur" }]
      }
    };
  },
  methods: {
    login() {
      
      this.$refs.ruleForm2.validate(valid => {
        if (valid) {

          this.$axios({
            method: "post",
            url: "/accounts/login",
            data: this.ruleForm2
          }).then(res => {
            this.$message.success('登录成功')
            this.$store.commit("user/setUserInfo", res.data);
            this.$router.push('/')
            // console.log(res);
          });
        }
      });
    }
  }
};
</script>

<style scoped lang='less'>
.demo-ruleForm {
  padding: 10px;
}
.loginBtn{
  width: 100%;
}
</style>